package com.xxg.imagemanager.service;

import lombok.Data;

import java.util.concurrent.locks.ReentrantLock;

/**
 * qps限流控制器
 *
 * @author xuxiaogang
 * @date 2021-12-14
 */
@Data
public class QpsController {
    /**
     * 拍他锁
     */
    private ReentrantLock reentrantLock;
    /**
     * 持续时间
     */
    private long t;
    /**
     * QPS计数器
     */
    private int count;
    /**
     * QPS使用上限
     */
    private int limit;

    public QpsController(int limit) {
        this.limit = limit;
        reentrantLock = new ReentrantLock();
        t = 0;
        count = 0;
    }

    /**
     * 计数器累加
     */
    public void increaseCount() {
        count++;
    }

    /**
     * 控制器重置
     */
    public void reset() {
        t = System.currentTimeMillis() + 1000;
        count = 1;
    }
}
